package controllers;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import models.User;
import play.Logger;
import play.libs.OAuth2;
import play.libs.WS;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.Scope.Session;
import play.mvc.With;

import com.google.gson.JsonObject;

import controllers.Secure.Security;

@Check("admin")
@With(Secure.class)
public class Users extends CRUD
{
	public static OAuth2	FACEBOOK	= new OAuth2(
												"https://graph.facebook.com/oauth/authorize",
												"https://graph.facebook.com/oauth/access_token",
												"388171657886328",
												"749b3e6691f50b63a665b2f87e4cf2f2");
/*
	public static void welcome_page()
	{

		render();
	}

	public static void home_page()
	{
		render();
	}

	public static void manage()
	{
		System.out.println("Running in Users.manage method");

		User u = getCurrentUser();
		JsonObject me = null;
		JsonObject me2 = null;
		if (u != null && !u.access_token1.isEmpty())
		{
			System.out.println("CODEEEEEEEEEEEEEE");
			me = WS.url("https://graph.facebook.com/me?access_token=%s",
					WS.encode(u.access_token1)).get().getJson()
					.getAsJsonObject();
		}
		if (u != null && !u.access_token2.isEmpty())
		{
			System.out.println("nooooooooooooooooo");

			me2 = WS.url("https://graph.facebook.com/me?access_token=%s",
					WS.encode(u.access_token2)).get().getJson()
					.getAsJsonObject();
		}
		String id1 = me.get("id").getAsString();
		String src1 = "https://graph.facebook.com/" + id1 + "/picture";

		String id2 = me2.get("id").getAsString();
		String src2 = "https://graph.facebook.com/" + id2 + "/picture";

		render(src1, src2, me, me2);
	}

	public static void auth()
	{
		System.out.println("Running in auth method");

		if (OAuth2.isCodeResponse())
		{
			User u = Application.getCurrentUser(); // the user currently logged
													// in
			System.out.println("FEL AWEEEEEEEEEEEEL");
			System.out.println("ACCESS TOKEN 1" + u.access_token1.toString());
			System.out.println("ACCESS TOKEN 2" + u.access_token2.toString());

			OAuth2.Response response = FACEBOOK.retrieveAccessToken(authURL());
			if (u.access_token1.isEmpty())
			{
				System.out.println("FIRST TOKEEEEEEEEEEEEEEEEEEN");
				System.out.println("ACCESS TOKEN 1"
						+ u.access_token1.toString());
				System.out.println("ACCESS TOKEN 2"
						+ u.access_token2.toString());

				u.access_token1 = response.accessToken;
				u.save();
			}
			else
			{
				System.out.println("SECOND TOKEEEEEEEEEEEEEEEEEEN");
				System.out.println("ACCESS TOKEN 1"
						+ u.access_token1.toString());
				System.out.println("ACCESS TOKEN 2"
						+ u.access_token2.toString());

				u.access_token2 = response.accessToken;
				u.save();
			}

			manage();
		}
		FACEBOOK.retrieveVerificationCode(authURL());
	}

	public static String authURL()
	{
		System.out.println("Running in Users.authURL method");

		return play.mvc.Router.getFullUrl("Users.auth");
	}

	public static void saveDescription()
	{

	}
*/
	/**
	 * This method gets the user that is currently loggedin in this session
	 * @author Rania Said
	 */
	public static User getCurrentUser()
	{
		System.out.println("Running in Users.getCurrentUser method");
		User user = User.findByUsername(Security.connected());
		System.out.println("The current user is " + user.username);
		return user;
	}

}